package com.wu.leetcode.leetcode1160;

/**
 * Leetcode1160-拼写单词
 *
 * @author lynn
 * @date 2020/10/31 - 23:18
 */
public class Solution {
    public static int countCharacters(String[] words, String chars) {

        char[] chs = chars.toCharArray();
        int[] ints = new int[26];

        for (char ch : chs) {
            ints[ch - 'a']++;
        }
        int length = 0;

        int[] arr;

        for (String word : words) {
            arr = new int[26];
            boolean flag = true;
            for (char c : word.toCharArray()) {
                arr[c - 'a']++;
                if (ints[c - 'a'] < arr[c - 'a'] ) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                length += word.length();
            }
        }
        return length;
    }

    public static void main(String[] args) {
        String[] words = new String[]{"cat","bt","hat","tree"};
        String str = "atach";
        int i = countCharacters(words, str);
        System.out.println(i);
    }
}
